package com.lexin.action.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import com.lexin.action.model.UserAccounts;
import com.lexin.common.base.TabelName;
import com.lexin.common.utils.BeanUtil;
import com.lexin.common.utils.DateUtil;
import com.lexin.common.utils.JdbcUtils;

@Repository
public class UserAccountsDaoImpl {
  
  /***
   * 根据ID查询账户
   * @param code
   * @return
   * @throws Exception
   */
  public UserAccounts select ( long id ) throws Exception {
    QueryRunner query = new QueryRunner(JdbcUtils.getDataSource());
    StringBuffer sql = new StringBuffer("SELECT * FROM " + TabelName.UserAccounts + " WHERE `id`='"+id+"'");
    return query.query(sql.toString(), new BeanHandler<UserAccounts>(UserAccounts.class));
  }
  
  /***
   * 根据实体类有赋值的内容作为条件查询所有符合条件的数据
   * @param seq
   * @return
   * @throws Exception
   */
  public List<UserAccounts> select ( Map<String,Object> search, String defNumVal ) throws Exception {
    QueryRunner query = new QueryRunner(JdbcUtils.getDataSource());
    StringBuffer sql = new StringBuffer("SELECT * FROM " + TabelName.UserAccounts + " WHERE ");
    int len = sql.length();
    
    for (String k : search.keySet()) {
    		Object v = search.get(k);
			if ( (v==null || v.equals(defNumVal))==false ) {
				sql.append( k+"='"+v.toString()+"' AND " );
			}
	}
    if (len>=sql.length()) {
		return new ArrayList<UserAccounts>();
	}
	sql.delete(sql.length()-4, sql.length());
    return query.query(sql.toString(), new BeanListHandler<UserAccounts>(UserAccounts.class));
  }
  
  /***
   * 更新指定表的序列信息
   * @param seq
   * @param code
   * @return
   * @throws Exception
   */
  public int update ( UserAccounts userAccounts, long id ) throws Exception {
    QueryRunner query = new QueryRunner(JdbcUtils.getDataSource());
    StringBuffer sql = new StringBuffer("UPDATE " + TabelName.UserAccounts+ " SET ");
    int len = sql.length();
    
    Map<String,Object> map = BeanUtil.getBeanMap(userAccounts);
    map.replace("create_date", 
    		DateUtil.format((Timestamp)map.get("create_date"), "yyyy-MM-dd HH:mm:ss"));
    for (String k : map.keySet()) {
    	if (map.get(k)!=null) {
    		Object v = map.get(k);
        	if (v!=null && BeanUtil.isNumberType(v.getClass())==true) {
        		sql.append("`"+k+"`='"+(map.get(k)==null?"0":map.get(k))+"',");
        	} else {
        		sql.append("`"+k+"`='"+(map.get(k)==null?"":map.get(k))+"',");
        	}
		}
    }
    if (len>=sql.length()) {
		return 0;
	}
    sql.deleteCharAt(sql.length()-1);
    return query.update(sql.toString());
  }
  
  /***
   * 添加新数据
   * @param seq
   * @return
   * @throws Exception
   */
  public int insert ( UserAccounts userAccounts ) throws Exception {
    QueryRunner query = new QueryRunner(JdbcUtils.getDataSource());
    StringBuffer sql = new StringBuffer("INSERT INTO " + TabelName.UserAccounts + " SET ");
    int len = sql.length();
    
    Map<String,Object> map = BeanUtil.getBeanMap(userAccounts); 
    map.replace("create_date", 
    		DateUtil.format((Timestamp)map.get("create_date"), "yyyy-MM-dd HH:mm:ss"));
    for (String k : map.keySet()) {
    	Object v = map.get(k);
    	if (v!=null && BeanUtil.isNumberType(v.getClass())==true) {
    		sql.append("`"+k+"`='"+(map.get(k)==null?"0":map.get(k))+"',");
		} else {
			sql.append("`"+k+"`='"+(map.get(k)==null?"":map.get(k))+"',");
		}
    }
    if (len>=sql.length()) {
		return 0;
	}
    sql.deleteCharAt(sql.length()-1);
    return query.update(sql.toString());
  }
  
//  public static void main(String[] args) throws Exception {
//	  UserAccountsDaoImpl uad = new UserAccountsDaoImpl();
//	  UserAccounts userAccounts = new UserAccounts();
//	  userAccounts.setId(1);
//	  userAccounts.setUsername("ddddd");
//	  userAccounts.setPassword("bbbb");
//	  
//	  System.out.println( uad.update(userAccounts,1) );
//	  
//	  System.out.println(uad.select(0) );
//	  
//	  userAccounts.setPassword("ccccc");
//	  System.out.println( uad.select(BeanUtil.getBeanMap(userAccounts), "-99") );
//  }
  
}
